package com.xb.loan.usercenter.account.impl;

import com.xb.loan.account.service.*;
import com.xb.loan.account.service.*;
import com.xb.loan.account.service.bean.AccountDeltaVO;
import com.xb.loan.account.service.bean.CouponInfoVO;
import com.xb.loan.account.service.constant.AccountConst;
import com.xb.loan.account.service.exception.IAccountServiceException;
import com.xb.loan.log.exception.BizException;
import com.xb.loan.usercenter.service.user.IUserService;
import com.xb.loan.usercenter.service.user.bean.UserInfoVO;
import com.xb.loan.util.log.DefaultLoggerFacotry;
import com.xb.loan.util.log.LogFormatUtil;
import com.xb.loan.util.mybatis.MyTransactional;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
	
	private static Logger logger = DefaultLoggerFacotry.getLogger(AccountServiceImpl.class);
	
	@Autowired
	private ICashService cashService;
	@Autowired
	private ILotCashService lotCashService;
	@Autowired
	private ICouponService couponService;
	@Autowired
	private IPointService pointService;
	@Autowired(required = false)
	private IUserService userService;

	@MyTransactional
	@Override
	public void addMoney(String foreignId,
                         UserInfoVO userInfoVO, AccountDeltaVO accountDeltaVO,
                         int billType, String accountPeriod) throws BizException {
		if(accountDeltaVO.getCash() > 0){//加现金
			cashService.addMoney(foreignId, userInfoVO, accountDeltaVO.getCash(), billType, accountPeriod);
//			if(billType == AccountConst.BillType_Exchage_Prize){//派奖
//				if(partnerInfoVO.getFeeType() == AccountConst.FeeType_ProxyPay){// 代付
//					// 渠道用户信息
//					UserInfoVO partnerUserInfoVO = userService.queryUserInfoById(partnerInfoVO.getPayUserid());
//					cashService.transfer(foreignId, userInfoVO, partnerUserInfoVO, accountDeltaVO.getCash(), AccountConst.FeeType_ProxyPay, 
//							AccountConst.BillType_Proxy_Exchage_Out, AccountConst.BillType_Proxy_Exchage_In, accountPeriod);
//				}else if(partnerInfoVO.getFeeType() == AccountConst.FeeType_CreditPay){// 垫付
//					cashService.deductMoney(foreignId, userInfoVO, accountDeltaVO.getCash(), AccountConst.FeeType_CreditPay, 
//							AccountConst.BillType_Credit_Exchage_Encash, accountPeriod);
//				}
//			}
		}
		if(accountDeltaVO.getLotcash() > 0){//加彩金
			lotCashService.addMoney(foreignId, userInfoVO, accountDeltaVO.getLotcash(), billType, accountPeriod);
		}
		if(accountDeltaVO.getCouponDeltas() != null){//加礼券
			for(CouponInfoVO couponInfoVO : accountDeltaVO.getCouponDeltas()){
				couponService.addCoupon(foreignId, userInfoVO, couponInfoVO, billType, accountPeriod);
			}
		}
		if(accountDeltaVO.getPoint() > 0){//加积分
			pointService.addPoint(foreignId, userInfoVO, accountDeltaVO.getPoint(), billType, accountPeriod);
		}
	}

	@MyTransactional
	@Override
	public void deductMoney(String foreignId,
                            UserInfoVO userInfoVO, AccountDeltaVO accountDeltaVO,
                            int billType, String accountPeriod) throws BizException {
		if(accountDeltaVO.getCash() > 0){//扣现金
			if(billType == AccountConst.BillType_Purchase_Lottery){// 购彩扣费
//				if(partnerInfoVO.getFeeType() == AccountConst.FeeType_ProxyPay){//代付
//					// 渠道用户信息
//					UserInfoVO partnerUserInfoVO = userService.queryUserInfoById(partnerInfoVO.getPayUserid());
//					cashService.transfer(foreignId, partnerUserInfoVO, userInfoVO, accountDeltaVO.getCash(), AccountConst.FeeType_ProxyPay, 
//							AccountConst.BillType_Proxy_Pay_Out, AccountConst.BillType_Proxy_Pay_In, accountPeriod);
//				}else if(partnerInfoVO.getFeeType() == AccountConst.FeeType_CreditPay){//垫付
//					// 渠道用户信息
//					UserInfoVO partnerUserInfoVO = userService.queryUserInfoById(partnerInfoVO.getPayUserid());
//					cashService.purchaseTransfer(foreignId, partnerUserInfoVO, userInfoVO, accountDeltaVO.getCash(), AccountConst.FeeType_CreditPay, 
//							AccountConst.BillType_Credit_Pay_Out, AccountConst.BillType_Credit_Pay_In, accountPeriod);
//				}
			}
			cashService.deductMoney(foreignId, userInfoVO, accountDeltaVO.getCash(), billType, accountPeriod);
		}
		if(accountDeltaVO.getLotcash() > 0){//扣彩金
			lotCashService.deductMoney(foreignId, userInfoVO, accountDeltaVO.getLotcash(), billType, accountPeriod);
		}
		if(accountDeltaVO.getCouponDeltas() != null){//扣礼券
			for(CouponInfoVO couponInfoVO : accountDeltaVO.getCouponDeltas()){
				couponService.deductCoupon(foreignId, userInfoVO, couponInfoVO.getCouponId(), couponInfoVO.getMoney(), billType, accountPeriod);
			}
		}
		if(accountDeltaVO.getPoint() > 0){//扣积分
			pointService.deductPoint(foreignId, userInfoVO, accountDeltaVO.getPoint(), billType, accountPeriod);
		}
	}

	@Override
	public void flushes(String userId, String foreignId, int billType) throws BizException {
		if(billType != AccountConst.BillType_Encash || billType != AccountConst.BillType_Purchase_Lottery){
			logger.error(LogFormatUtil.getFormatMsg(new String[]{"module", "foreignId", "billType", "msg"},
					new Object[]{"account", foreignId, billType, IAccountServiceException.invalidFlushesType.getMessage()}));
			throw IAccountServiceException.invalidFlushesType;
		}
		
		cashService.flushes(userId, foreignId, billType);
		if(billType == AccountConst.BillType_Purchase_Lottery){
			//TODO代付与垫付
		}
		lotCashService.flushes(userId, foreignId, billType);
		couponService.flushes(userId, foreignId, billType);
		pointService.flushes(userId, foreignId, billType);
	}

}
